package com.shop.zkmall.app.controller.a;
import com.shop.zkmall.app.service.buyer.CereBuyerUserService;
import jakarta.servlet.http.HttpServletRequest;  // ✔ 正确的

import com.shop.zkmall.app.dao.buyer.CereBuyerUserDAO;
import com.shop.zkmall.app.domain.BankParam;
import com.shop.zkmall.commons.constant.CoReturnFormat;
import com.shop.zkmall.commons.domain.buyer.CereBuyerUser;
import com.shop.zkmall.commons.result.Result;
import com.shop.zkmall.commons.utils.EmptyUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("bank")
@RequiredArgsConstructor
@Tag(name = "银行卡模块", description = "银行卡模块")
public class BankController {


    private final CereBuyerUserService cereBuyerUserService;
    private final CereBuyerUserDAO cereBuyerUserDAO;


    /**
     * 获取银行卡列表
     */
    @RequestMapping(value = "getBank")
    @Operation(summary = "获取银行卡列表")
    public Result<List<BankParam>> getBank() {
        HttpServletRequest request =
                (HttpServletRequest) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
        }
        List<BankParam> bankParams = cereBuyerUserDAO.selBank(user);
        return new Result<>(bankParams);
    }



    /**
     * 添加银行卡
     */
    @RequestMapping(value = "addBank")
    @Operation(summary = "添加银行卡")
    public Result<String> addBank(BankParam param) {
        HttpServletRequest request =
                (HttpServletRequest) ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("Authorization");
        CereBuyerUser user = null;
        if (!EmptyUtils.isEmpty(token)) {
            //根据token查询用户信息
            user = cereBuyerUserService.findByToken(token);
            param.setBuyerUserId(user.getBuyerUserId().toString());
        }
        cereBuyerUserDAO.addBank(param);
        return new Result<>(CoReturnFormat.SUCCESS);
    }
}
